package world.liuru.poem.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import world.liuru.poem.utils.HttpUtils;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ocr")
public class OcrController {
    @RequestMapping("/getOcrImgInfo")
    public String getOcrImgInfo(@RequestBody Map<String,String> ocrInfo){

        String host = "https://tysbgpu.market.alicloudapi.com";
        String path = "/api/predict/ocr_general";
        String method = "POST";
        String appcode = "ae3417cefdc1479b947428b01dccb210";
        Map<String, String> headers = new HashMap<>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        Map<String,Object> map = new HashMap<>();
        map.put("image",ocrInfo.get("image"));
        Map<String,Object> map1 = new HashMap<>();
        map1.put("output_prob",true);
        map1.put("output_keypoints",false);
        map1.put("skip_detection",false);
        map1.put("without_predicting_direction",false);
        map1.put("language","x");
        map.put("configure",map1);
        String bodys = JSONObject.toJSONString(map);
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            return EntityUtils.toString(response.getEntity());
            //获取response的body
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
